package cn.lsngo.manager;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.lsngo.manager.greendao.ElectricBean;

/**
 * @author : admin
 * @Title :
 * @date : 2021/12/23 15:09
 */
public class HistoryListAdapter extends RecyclerView.Adapter<HistoryListAdapter.HistoryListViewHolder> {
    private Context mContext;
    private List<ElectricBean> mList;
    private OnClick onClick;

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    public HistoryListAdapter(Context mContext, List<ElectricBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @NonNull
    @Override
    public HistoryListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_history_list, parent, false);
        return new HistoryListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HistoryListViewHolder holder, int position) {
        ElectricBean bean = mList.get(position);
        String createTime = bean.getCreateTime();
        String amountMoney = bean.getAmountMoney();
        holder.mTvTime.setText(String.format("%s", createTime));
        holder.mTvAmount.setText(String.format("money:%s", amountMoney));

        holder.itemView.setOnClickListener(v -> {
            if (onClick != null) {
                onClick.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public void setList(List<ElectricBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }


    public class HistoryListViewHolder extends RecyclerView.ViewHolder {
        private TextView mTvTime;
        private TextView mTvAmount;

        public HistoryListViewHolder(@NonNull View itemView) {
            super(itemView);
            mTvTime = (TextView) itemView.findViewById(R.id.tv_time);
            mTvAmount = (TextView) itemView.findViewById(R.id.tv_amount);
        }
    }

    public interface OnClick {
        void onItemClick(int position);
    }
}
